package cn.chencq.utils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;

import com.alibaba.druid.util.StringUtils;

public class ThRequestUtils {

	public static final String KEY = "23AF1HG6ERTJ9azdf3h21rtj8SFHYKU8974";

	public static String thRequest(String opt, String auth, String info) throws IOException {
		StringBuffer url = new StringBuffer();
		url.append("http://www.th0168.com/ajax/AppGateway.ashx").append("?");
		url.append("opt=").append(opt);
		if (!StringUtils.isEmpty(auth)) {
			url.append("&").append("auth=").append(URLEncoder.encode(auth));
		}
		if (!StringUtils.isEmpty(info)) {
			url.append("&").append("info=").append(URLEncoder.encode(info));
		}
		String res = HttpClientUtil.sendHttpPost(url.toString());
		return res;
	}

	public static String bytesToHex(byte[] paramArrayOfByte) {
		StringBuffer localStringBuffer = new StringBuffer();
		for (int i = 0; i < paramArrayOfByte.length; i++) {
			int j = paramArrayOfByte[i];
			if (j < 0)
				j += 256;
			if (j < 16)
				localStringBuffer.append("0");
			localStringBuffer.append(Integer.toHexString(j));
		}
		return localStringBuffer.toString().toUpperCase();
	}

	private static byte[] getContentBytes(String paramString1, String paramString2) {
		if ((paramString2 == null) || ("".equals(paramString2)))
			return paramString1.getBytes();
		try {
			byte[] arrayOfByte = paramString1.getBytes(paramString2);
			return arrayOfByte;
		} catch (UnsupportedEncodingException localUnsupportedEncodingException) {
		}
		throw new RuntimeException("MD5签名过程中出现错误,指定的编码集不对,您目前指定的编码集是:" + paramString2);
	}

	public static String md5(String paramString) {
		return new String(Hex.encodeHex(DigestUtils.md5(getContentBytes(paramString, "utf-8"))));
	}

}
